<template>
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>注册</span>
        </div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirm">
                <el-input type="password" v-model="ruleForm.confirm" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import InputElement from '../components/InputElement.vue';
import { usernameExist, reg  } from '../services/user';
export default {
    components: {
        InputElement
    },
    data() {
        var validateUsername = async (rule, value, callback) => {
            let data = await usernameExist(value);
            if (data.status == 1) {
                callback();
            } else {
                callback(new Error('重名'));
            }
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.confirm !== '') {
                    this.$refs.ruleForm.validateField('confirm');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pwd) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                pwd: '',
                confirm: '',
                username: ''
            },
            rules: {
                pwd: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                confirm: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                username: [
                    { required: true, message: "用户名不能为空" },
                    { pattern: /^\w{6,20}$/, message: "用户名必须位6-20位字母或数字组成" },
                    { validator: validateUsername, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    // alert('submit!');
                    await reg(this.ruleForm.username, this.ruleForm.pwd);
                    this.$alert('注册成功', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$router.push("/login");
                        }
                    });

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>
<style>
.box-card {
    width: 500px;
    margin: auto;
}
</style>